﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Ninject;
using Ninject.Modules;
using Ninject.Planning.Bindings;
using Ninject.Web.Common;
using YBBEST.Office365Portal.Core;

namespace YBBEST.Office365Portal.MvcWebRole
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : NinjectHttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            //Test route
//            routes.MapRoute(
//                "ListRoute", // Route name
//                "list/{listTitle}", // URL with parameters
//                new { controller = "list", action = "Search"} // Parameter defaults
//            );

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
            
        }

        protected override IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            kernel.Load(Assembly.GetExecutingAssembly());
            //Register your custom ProxyModules with Ninject
            kernel.Load
            (
                new List<NinjectModule>
                {
                    new ListsProxyModule(),
                    new MeetingsProxyModule(),
                    new CopyProxyModule()
                }
            );
            return kernel;
        }

        protected override void OnApplicationStarted()
        {
            base.OnApplicationStarted();
            AreaRegistration.RegisterAllAreas();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    }

    internal class MeetingsProxyModule : NinjectModule
    {
        public override void Load()
        {
            Bind<IMeetingRepository>().To<MeetingRepository>().WithConstructorArgument("configuration",new ProxyConfiguration());
        }
    }

    internal class CopyProxyModule : NinjectModule
    {
        public override void Load()
        {
            Bind<ICopyRepository>().To<CopyRepository>().WithConstructorArgument("configuration", new ProxyConfiguration());
        }
    }

    internal class ListsProxyModule : NinjectModule
    {
        public override void Load()
        {
            var proxy = new ProxyConfiguration();

            Bind<IMsOnlineClaimsHelper>().To<MsOnlineClaimsHelper>()
                .WithConstructorArgument("username", proxy.Username)
                .WithConstructorArgument("password", proxy.Password)
                .WithConstructorArgument("spoSiteUrl", proxy.SiteUrl);

            Bind<IHttpRequestMessageBuilder>().To<HttpMessageBuilder>();

            Bind<IServiceConnection>().To<ServiceConnection>().WithConstructorArgument("configuration",proxy);

            Bind<IListsRepository>().To<ListsRespository>().WithConstructorArgument("configuration", proxy);
        }
    }
}